// C++17 结构化绑定 和 初始化列表 详解

#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
#include <tuple>
using namespace std;

// ============== 1. 结构化绑定 (Structured Binding) ==============
// C++17 引入的特性，用于解构复合类型

void structuredBindingExamples() {
    cout << "=== 结构化绑定示例 ===" << endl;
    
    // 1.1 解构 pair
    pair<int, string> p = {42, "hello"};
    auto [num, str] = p;  // 结构化绑定，等价于 int num = p.first; string str = p.second;
    cout << "pair: " << num << ", " << str << endl;
    
    // 1.2 解构 tuple
    tuple<int, double, string> t = {1, 3.14, "world"};
    auto [a, b, c] = t;
    cout << "tuple: " << a << ", " << b << ", " << c << endl;
    
    // 1.3 遍历 map 时的结构化绑定（最常用场景）
    unordered_map<string, int> freq = {{"apple", 3}, {"banana", 2}, {"orange", 1}};
    
    cout << "传统方式遍历 map:" << endl;
    for (auto it = freq.begin(); it != freq.end(); ++it) {
        cout << it->first << ": " << it->second << endl;
    }
    
    cout << "使用 auto 遍历:" << endl;
    for (auto& kv : freq) {
        cout << kv.first << ": " << kv.second << endl;
    }
    
    cout << "使用结构化绑定遍历（推荐）:" << endl;
    for (auto& [key, value] : freq) {  // 这里的 [key, value] 就是结构化绑定
        cout << key << ": " << value << endl;
    }
    
    // 1.4 结构化绑定的引用版本
    cout << "修改值的例子:" << endl;
    for (auto& [key, value] : freq) {
        value *= 2;  // 可以修改原始值
    }
    for (const auto& [key, value] : freq) {
        cout << key << ": " << value << endl;
    }
}

// ============== 2. 初始化列表 (Initializer List) ==============
// 用花括号 {} 进行初始化

void initializerListExamples() {
    cout << "\n=== 初始化列表示例 ===" << endl;
    
    // 2.1 基本类型初始化
    int x{42};        // 统一初始化语法
    double y{3.14};
    cout << "基本类型: " << x << ", " << y << endl;
    
    // 2.2 容器初始化
    vector<int> vec{1, 2, 3, 4, 5};
    cout << "vector: ";
    for (int v : vec) cout << v << " ";
    cout << endl;
    
    // 2.3 pair 初始化
    pair<int, string> p1{100, "test"};  // 使用初始化列表
    pair<int, string> p2(200, "test2"); // 使用构造函数
    cout << "pair1: " << p1.first << ", " << p1.second << endl;
    cout << "pair2: " << p2.first << ", " << p2.second << endl;
    
    // 2.4 map 初始化
    unordered_map<string, int> map1{{"a", 1}, {"b", 2}, {"c", 3}};
    cout << "map: ";
    for (const auto& [k, v] : map1) {
        cout << k << "=" << v << " ";
    }
    cout << endl;
    
    // 2.5 在函数调用中使用初始化列表
    auto printPair = [](const pair<int, string>& p) {
        cout << "函数参数: " << p.first << ", " << p.second << endl;
    };
    
    printPair({999, "direct"});  // 直接传递初始化列表
}

// ============== 3. 在优先级队列中的应用 ==============

void priorityQueueExample() {
    cout << "\n=== 优先级队列中的应用 ===" << endl;
    
    // 模拟 LeetCode 347 题的场景
    unordered_map<int, int> freqMap{{1, 3}, {2, 2}, {3, 1}};
    
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
    
    cout << "原始频率统计:" << endl;
    for (const auto& [num, freq] : freqMap) {  // 结构化绑定解构 map 的键值对
        cout << "数字 " << num << " 出现 " << freq << " 次" << endl;
        pq.push({freq, num});  // 初始化列表创建 pair
    }
    
    cout << "\n小顶堆中的元素（按频率排序）:" << endl;
    auto temp_pq = pq;  // 复制一份用于显示
    while (!temp_pq.empty()) {
        auto [freq, num] = temp_pq.top();  // 结构化绑定解构 pair
        cout << "频率: " << freq << ", 数字: " << num << endl;
        temp_pq.pop();
    }
}

// ============== 4. 语法对比和最佳实践 ==============

void bestPractices() {
    cout << "\n=== 语法对比和最佳实践 ===" << endl;
    
    unordered_map<string, int> data{{"apple", 5}, {"banana", 3}};
    
    cout << "1. 传统方式（C++11之前）:" << endl;
    for (unordered_map<string, int>::iterator it = data.begin(); it != data.end(); ++it) {
        cout << "  " << it->first << ": " << it->second << endl;
    }
    
    cout << "2. auto 简化（C++11）:" << endl;
    for (auto it = data.begin(); it != data.end(); ++it) {
        cout << "  " << it->first << ": " << it->second << endl;
    }
    
    cout << "3. 范围for循环（C++11）:" << endl;
    for (const auto& item : data) {
        cout << "  " << item.first << ": " << item.second << endl;
    }
    
    cout << "4. 结构化绑定（C++17，推荐）:" << endl;
    for (const auto& [key, value] : data) {
        cout << "  " << key << ": " << value << endl;
    }
    
    cout << "\n初始化方式对比:" << endl;
    // 不同的 pair 创建方式
    pair<int, string> p1 = make_pair(1, "old");     // C++98 方式
    pair<int, string> p2(2, "constructor");         // 构造函数
    pair<int, string> p3{3, "modern"};              // C++11 初始化列表（推荐）
    auto p4 = pair{4, "auto"};                      // C++17 类模板参数推导
    
    cout << "  p1: " << p1.first << ", " << p1.second << endl;
    cout << "  p2: " << p2.first << ", " << p2.second << endl;
    cout << "  p3: " << p3.first << ", " << p3.second << endl;
    cout << "  p4: " << p4.first << ", " << p4.second << endl;
}

int main() {
    structuredBindingExamples();
    initializerListExamples();
    priorityQueueExample();
    bestPractices();
    
    cout << "\n=== 总结 ===" << endl;
    cout << "• auto& [a, b] : 结构化绑定，用于解构 pair/tuple/数组等" << endl;
    cout << "• {a, b}       : 初始化列表，用于创建对象" << endl;
    cout << "• 两者经常配合使用，让代码更简洁易读" << endl;
    
    return 0;
}

/*
编译和运行:
g++ -std=c++17 cpp_syntax_examples.cc -o syntax_demo
./syntax_demo

注意事项:
1. 结构化绑定需要 C++17 支持
2. 初始化列表是 C++11 特性
3. 在 LeetCode 等在线平台，通常支持 C++17
4. 使用这些现代语法可以让代码更清晰、更安全
*/